﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using log4net;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Reflection;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainFileKnowledgeKeyController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        /// <summary>
        /// 文件类型的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/TrainFileKnowledgeKey/Add")]
        [HttpPost]
        public IHttpActionResult Add(Model.TrainFileKnowledgeKey model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.TrainFileKnowledgeKey _KnowledgeKey = new BLL.TrainFileKnowledgeKey();
                if (!_KnowledgeKey.Exists(model.KKName))
                {
                    if (new IPR_StudyMeetingMrgSys.BLL.TrainFileKnowledgeKey().Add(model) > 0)
                    {
                        resultMsg.Info = "增加成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "增加失败";
                    }
                }
                else
                {
                    resultMsg.Info = "名称已经存在";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 文件类型查询
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="startindex"></param>
        /// <param name="endindex"></param>
        /// <returns></returns>
        //[HttpGet]
        //[Route("api/TrainFileKnowledgeKey/Search")]
        //public IHttpActionResult Search(string swhere, int startindex, int endindex)
        //{
        //    ResultMsg resultMsg = new ResultMsg();
        //    try
        //    {
        //        bool blExist = false;
        //        DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainFileKnowledgeKey().GetListByPage(swhere, "KKID", startindex, endindex);
        //        if (dsSearch != null)
        //            if (dsSearch.Tables.Count > 0)
        //                if (dsSearch.Tables[0].Rows.Count > 0)
        //                {
        //                    //返回token信息
        //                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
        //                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
        //                    resultMsg.Data = dsSearch.Tables[0];
        //                    blExist = true;
        //                }
        //        if (!blExist)
        //        {
        //            //返回token信息
        //            resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
        //            resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
        //            resultMsg.Data = "查询结果不存在！";
        //        }
        //    }
        //    catch (Exception exc)
        //    {
        //        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
        //        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
        //        resultMsg.Data = "查询异常！" + exc.Message;
        //    }
        //    return Json(resultMsg);
        //}

        /// <summary>
        /// 文件类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainFileKnowledgeKey/Edit")]
        public IHttpActionResult Edit(Model.TrainFileKnowledgeKey model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileKnowledgeKey().Update(model))
                {
                    resultMsg.Info = "编辑成功";
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "编辑失败";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/TrainFileKnowledgeKey/Del")]
        public IHttpActionResult Del(Model.TrainFileKnowledgeKey model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileKnowledgeKey().Delete(model.KKID))
                {
                    resultMsg.Info = "删除成功";
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "删除失败";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 知识点查询
        /// </summary>
        [HttpPost]
        [Route("api/TrainFileKnowledgeKey/SelList")]
        public IHttpActionResult SelList()
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainFileKnowledgeKey().GetList("");
                if (dsSearch != null && dsSearch.Tables.Count > 0)

                    if (dsSearch.Tables[0].Rows.Count > 0)
                    {
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = dsSearch.Tables[0];
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "未获取到相应数据";
                    }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

    }
}
